/*
* Copyright 2010 ANDAGO INGENIERIA S.L.
*
* Licensed under the EUPL, Version 1.1 only (the "Licence");
* You may not use this work except in compliance with the
* Licence.
* You may obtain a copy of the Licence at:
*
* http://ec.europa.eu/idabc/eupl
*
* Unless required by applicable law or agreed to in
* writing, software distributed under the Licence is
* distributed on an "AS IS" basis,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied.
* See the Licence for the specific language governing
* permissions and limitations under the Licence.
*/

package com.andago.services;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.kxml2.kdom.Document;
import org.kxml2.kdom.Element;
import org.kxml2.kdom.Node;

import android.util.Log;

import com.andago.bean.Measurement;

public class VitalSignsWS {

	private String LOG_CAT = "VitalSignsWS";

	private static String SOAP_ACTION = "";
	private static String METHOD_NAME = "addVitalSign";
	private static String NAMESPACE = "http://healthassistant.andago.com/services/datasource/vitalsigns";
	private static String CCR_NAMESPACE = "urn:astm-org:CCR";
	private static String ADDRESSING_NAMESPACE = "http://www.w3.org/2005/08/addressing";
	private static String OHA_PARAMETERS = "http://oha.andago.com/healthassistant/datasource/parameters";

	// URL de OHA
	private static String URL = "http://oha-solution-demo.andago.com/OHA-Services/VitalSignsWS?wsdl";	

	private String userID;

	public VitalSignsWS(String user) {

		userID = user;
	}

	private Element createSecurityHeader() {
		/*
		 * 
		 * <ReferenceParameters xmlns="http://www.w3.org/2005/08/addressing">
		 * 		<User:User xmlns="http://oha.andago.com/healthassistant/datasource/parameters"
		 * 				xmlns:User="http://oha.andago.com/healthassistant/datasource/parameters">10607</User:User> 
		 * 		<CasTicket:CasTicket xmlns="http://oha.andago.com/healthassistant/datasource/parameters"
		 * 				xmlns:CasTicket="http://oha.andago.com/healthassistant/datasource/parameters">ticket</CasTicket:CasTicket> 
		 * 		<Entity:Entity xmlns="http://oha.andago.com/healthassistant/datasource/parameters"
		 * 				xmlns:Entity="http://oha.andago.com/healthassistant/datasource/parameters">dssample</Entity:Entity> 
		 * </ReferenceParameters>
		 */

		Document doc = new Document();		
		Element reference = doc.createElement(ADDRESSING_NAMESPACE, "ReferenceParameters");		

		Element user = doc.createElement(OHA_PARAMETERS, "User");
		user.addChild(Node.TEXT, userID);

		Element casTicket = doc.createElement(OHA_PARAMETERS, "CasTicket");
		casTicket.addChild(Node.TEXT, "ticket");

		Element entity = doc.createElement(OHA_PARAMETERS, "Entity");
		entity.addChild(Node.TEXT, "dssample");

		reference.addChild(Node.ELEMENT, user);
		reference.addChild(Node.ELEMENT, casTicket);
		reference.addChild(Node.ELEMENT, entity);

		return reference;
	}

	private Element createBody(Measurement m) {

		Document doc = new Document();

		Element addVitalSigns = doc.createElement(NAMESPACE, METHOD_NAME);

		Element vitalSign = new Element();
		vitalSign.setName("VitalSign");

		/*
		 * <ns2:DateTime> 
		 * 		<ns2:Type> 
		 * 			<ns2:Text>MAIN_DATE</ns2:Text> 
		 * 		</ns2:Type>
		 * 		<ns2:ExactDateTime>2010-01-20T10:00:00.000+01:00</ns2:ExactDateTime>
		 * </ns2:DateTime>
		 */
		Element datetime = doc.createElement(CCR_NAMESPACE, "DateTime");
		Element typeDate = doc.createElement(CCR_NAMESPACE, "Type");
		Element textT = doc.createElement(CCR_NAMESPACE, "Text");
		textT.addChild(Node.TEXT, "MAIN_DATE");
		typeDate.addChild(Node.ELEMENT, textT);
		Element exactTime = doc.createElement(CCR_NAMESPACE, "ExactDateTime");
		String date = m.getDate();
		String fecha= date+"+01:00";
		System.out.println("Fechaaaaaaaa " + fecha);
		//String actualDate = date.substring(0, 26) + ":" + date.substring(26,28);
		exactTime.addChild(Node.TEXT,fecha);
		datetime.addChild(Node.ELEMENT, typeDate);
		datetime.addChild(Node.ELEMENT, exactTime);

		/*
		 * <ns2:Type> 
		 * 		<ns2:Text>OXYGEN</ns2:Text> 
		 * </ns2:Type>
		 */
		
		/*
		 * Los servicios de Andago solo aceptan las variables
		 * 
		 * WEIGHT
		 * TEMPERATURE
		 * OXYGEN
		 * HEART_RATE
		 * BLOOD_PRESSURE
		 */


		Element type = doc.createElement(CCR_NAMESPACE, "Type");
		Element textType = doc.createElement(CCR_NAMESPACE, "Text");
		textType.addChild(Node.TEXT, m.getVariable());
		type.addChild(Node.ELEMENT, textType);

		/*
		 * <ns2:Status> 
		 * 		<ns2:Text>90</ns2:Text> 
		 * </ns2:Status>
		 */

		Element status = doc.createElement(CCR_NAMESPACE, "Status");
		Element textStatus = doc.createElement(CCR_NAMESPACE, "Text");
		textStatus.addChild(Node.TEXT, m.getValue());
		status.addChild(Node.ELEMENT, textStatus);

		/*
		 * <ns2:Source> 
		 * 		<ns2:Description> 
		 * 			<ns2:Text>FORM</ns2:Text>
		 * 		</ns2:Description> 
		 * 		<ns2:Actor> 
		 * 			<ns2:ActorID>75</ns2:ActorID>
		 * 		</ns2:Actor> 
		 * </ns2:Source>
		 */

		
		
		
		String question="";
		String form="";
		
		if (m.getVariable().startsWith("TEMPERATURE")) {
			form="123";
			question="124";
		}
		else if (m.getVariable().startsWith("WEIGHT")) {
			form="126";
			question="127";
		}
		else if (m.getVariable().startsWith("BLOOD_PRESSURE")){
			form="114";
			question="115";
		}
		else if (m.getVariable().startsWith("HEART_RATE")) {
			form="117";
			question="118";			
		}
		else if (m.getVariable().startsWith("OXYGEN")) {
			form="120";
			question="121";
		}
		
		Element sourceForm = doc.createElement(CCR_NAMESPACE, "Source");
		Element desF = doc.createElement(CCR_NAMESPACE, "Description");
		Element textF = doc.createElement(CCR_NAMESPACE, "Text");
		textF.addChild(Node.TEXT, "FORM");
		desF.addChild(Node.ELEMENT, textF);
		Element actorF = doc.createElement(CCR_NAMESPACE, "Actor");
		Element actorIDF = doc.createElement(CCR_NAMESPACE, "ActorID");
		actorIDF.addChild(Node.TEXT, form);
		actorF.addChild(Node.ELEMENT, actorIDF);
		sourceForm.addChild(Node.ELEMENT, desF);
		sourceForm.addChild(Node.ELEMENT, actorF);

		/*
		 * <ns2:Source> 
		 * 		<ns2:Description> 
		 * 			<ns2:Text>QUESTION</ns2:Text>
		 * 		</ns2:Description> 
		 * 		<ns2:Actor> 
		 * 			<ns2:ActorID>76</ns2:ActorID>
		 * 		</ns2:Actor> 
		 * </ns2:Source>
		 */

		Element sourceQ = doc.createElement(CCR_NAMESPACE, "Source");
		Element desQ = doc.createElement(CCR_NAMESPACE, "Description");
		Element textQ = doc.createElement(CCR_NAMESPACE, "Text");
		textQ.addChild(Node.TEXT, "QUESTION");
		desQ.addChild(Node.ELEMENT, textQ);
		Element actorQ = doc.createElement(CCR_NAMESPACE, "Actor");
		Element actorIDQ = doc.createElement(CCR_NAMESPACE, "ActorID");
		actorIDQ.addChild(Node.TEXT, question);
		actorQ.addChild(Node.ELEMENT, actorIDQ);
		sourceQ.addChild(Node.ELEMENT, desQ);
		sourceQ.addChild(Node.ELEMENT, actorQ);

		/*
		 * <ns2:Source> 
		 * 		<ns2:Description> 
		 * 			<ns2:Text>DEVICE</ns2:Text>
		 *		</ns2:Description> 
		 *</ns2:Source>
		 */

		Element sourceDevice = doc.createElement(CCR_NAMESPACE, "Source");
		Element desD = doc.createElement(CCR_NAMESPACE, "Description");
		Element textD = doc.createElement(CCR_NAMESPACE, "Text");
		textD.addChild(Node.TEXT, "DEVICE");
		desD.addChild(Node.ELEMENT, textD);
		sourceDevice.addChild(Node.ELEMENT, desD);

		/*
		 * <ns2:InternalCCRLink>
		 * 		<ns2:LinkRelationship>DIAGNOSIS</ns2:LinkRelationship>
		 * </ns2:InternalCCRLink> 
		 * 
		 * <ns2:InternalCCRLink>
		 * 		<ns2:LinkRelationship>ENCOUNTER</ns2:LinkRelationship>
		 * </ns2:InternalCCRLink>
		 */

		Element internalCCRLink = doc.createElement(CCR_NAMESPACE,"InternalCCRLink");
		Element linkRelationship = doc.createElement(CCR_NAMESPACE,"LinkRelationship");
		linkRelationship.addChild(Node.TEXT, "DIAGNOSIS");
		internalCCRLink.addChild(Node.ELEMENT, linkRelationship);

		Element internalCCRLink2 = doc.createElement(CCR_NAMESPACE,"InternalCCRLink");
		Element linkRelationship2 = doc.createElement(CCR_NAMESPACE,"LinkRelationship");
		linkRelationship2.addChild(Node.TEXT, "ENCOUNTER");
		internalCCRLink2.addChild(Node.ELEMENT, linkRelationship2);

		vitalSign.addChild(Node.ELEMENT, datetime);
		vitalSign.addChild(Node.ELEMENT, type);
		vitalSign.addChild(Node.ELEMENT, status);
		vitalSign.addChild(Node.ELEMENT, sourceForm);
		vitalSign.addChild(Node.ELEMENT, sourceQ);
		vitalSign.addChild(Node.ELEMENT, sourceDevice);
		vitalSign.addChild(Node.ELEMENT, internalCCRLink);
		vitalSign.addChild(Node.ELEMENT, internalCCRLink2);

		addVitalSigns.addChild(Node.ELEMENT, vitalSign);
		return addVitalSigns;
	}

	public void sendVitalSigns(Measurement m) throws Exception {

		SoapEnvelope envelope = new SoapEnvelope(SoapEnvelope.VER11);
		envelope.headerOut = new Element[] { this.createSecurityHeader() };
		envelope.bodyOut = this.createBody(m);

		HttpTransportSE ht = new HttpTransportSE(URL);

		ht.call(SOAP_ACTION, envelope);

		System.out.println(envelope.bodyIn.toString());

		try {
			Log.e(LOG_CAT, "RESPUESTA: " + envelope.bodyIn.toString());
		} catch (Exception e) {
			e.printStackTrace();
		}

	}
	
	public boolean checkVariable(String measure){
		
		if (measure.startsWith("TEMPERATURE")) return true;
		else if (measure.startsWith("WEIGHT")) return true;
		else if (measure.startsWith("BLOOD_PRESSURE")) return true;
		else if (measure.startsWith("HEART_RATE"))return true;
		else if (measure.startsWith("OXYGEN")) return true;
		else return false;
	}
		

}
